import Taro, { Config, Component } from '@tarojs/taro'
import { RichText, View } from '@tarojs/components'
import './index.scss'
import { formatHtml } from "@/utils/utils"
import { Loading } from "@/components/index"
import { getNewsDetail } from '@/api/newsApi'

export default class Index extends Component {
  state: any
  config: Config = {
    backgroundTextStyle: "dark",
    navigationBarTitleText: ''
  }

  constructor () {
    super(...arguments)
    const { params } = this.$router
    if (params.name) {
      Taro.setNavigationBarTitle({
        title: params.name
      })
    }
    this.state = {
      id: params.id,
      loading: true,
      newsInfo: {}
    }
  }

  componentWillMount() {
    this.getInfo().then()
  }

  //  获取资讯信息
  async getInfo() {
    getNewsDetail(this.state.id).then(({ data }) => {
      this.setState({
        loading: false,
        newsInfo: data
      })
    })
  }

  render() {
    const { loading, newsInfo } = this.state
    return (<View className='page'>
      {loading && <Loading isCover/>}
      <View className='rich-text-wrap'>
        <RichText nodes={formatHtml(newsInfo.content)} />
      </View>
    </View>)
  }
}
